package com.icesoft.system.sender.email.impl;

import com.icesoft.framework.core.exception.BusinessException;
import com.icesoft.system.sender.email.EmailSender;
import com.icesoft.system.types.MailTypeDictionary;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.internet.MimeMessage;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.MessageFormat;
import java.util.Properties;

@Slf4j
@Service
@RequiredArgsConstructor
public class EmailServiceImpl implements EmailSender {

	private static JavaMailSenderImpl javaMailSender;

	private final MailTypeDictionary mailTypeDictionary;

	private void initMailSender() {
		javaMailSender = new JavaMailSenderImpl();
		javaMailSender.setHost(mailTypeDictionary.host);
		javaMailSender.setUsername(mailTypeDictionary.username);
		javaMailSender.setPassword(mailTypeDictionary.password);
		javaMailSender.setDefaultEncoding("Utf-8");
		Properties properties = new Properties();
		properties.setProperty("mail.debug", "true");
		properties.setProperty("mail.smtp.ssl.enable", "true");
		javaMailSender.setJavaMailProperties(properties);
	}

	public String buildContent(String title, String email) {
		//加载邮件html模板
		org.springframework.core.io.Resource resource = new ClassPathResource("META-INF/ftl/mailtemplate.ftl");
		InputStream inputStream = null;
		BufferedReader fileReader = null;
		StringBuilder buffer = new StringBuilder();
		String line = "";
		try {
			inputStream = resource.getInputStream();
			fileReader = new BufferedReader(new InputStreamReader(inputStream));
			while ((line = fileReader.readLine()) != null) {
				if (line.contains("{{SYSTEM_NAME}}")) {
					line = line.replace("{{SYSTEM_NAME}}", mailTypeDictionary.systemName);
				}
				if (line.contains("{{SYSTEM_EMAIL}}")) {
					line = line.replace("{{SYSTEM_EMAIL}}", mailTypeDictionary.systemEmail);
				}
				buffer.append(line);
			}
		} catch (Exception e) {
			log.error("发送邮件读取模板失败", e);
		} finally {
			if (fileReader != null) {
				try {
					fileReader.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		//替换html模板中的参数
		return MessageFormat.format(buffer.toString(), title);
	}

	@Override
	public void sendEmailCode(String email, String code) throws Exception {
		if (javaMailSender == null) {
			this.initMailSender();
		}
		MimeMessage message = javaMailSender.createMimeMessage();
		try {
			MimeMessageHelper helper = new MimeMessageHelper(message, true);
			helper.setSubject("主题");
			helper.setText(buildContent(code, email), true);
			helper.setTo(email);
			helper.setFrom(mailTypeDictionary.username);
			javaMailSender.send(message);
		} catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
	}

	@Override
	public void sendEmail(String email, String msg) throws Exception {
		if (javaMailSender == null) {
			this.initMailSender();
		}
		MimeMessage message = javaMailSender.createMimeMessage();
		try {
			MimeMessageHelper helper = new MimeMessageHelper(message, true);
			helper.setSubject("主题");
			helper.setText(msg, false);
			helper.setTo(email);
			helper.setFrom(mailTypeDictionary.username);
			javaMailSender.send(message);
		} catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
	}

	@Override
	public void sendEmailHtml(String email, String html) throws Exception {
		if (javaMailSender == null) {
			this.initMailSender();
		}
		MimeMessage message = javaMailSender.createMimeMessage();
		try {
			MimeMessageHelper helper = new MimeMessageHelper(message, true);
			helper.setSubject("主题");
			helper.setText(html, true);
			helper.setTo(email);
			helper.setFrom(mailTypeDictionary.username);
			javaMailSender.send(message);
		} catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
	}
}
